package com.guaju.yueshangLife.activity.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.AddAddressActivity;
import com.guaju.yueshangLife.activity.CaculateActivity;
import com.guaju.yueshangLife.adapter.AddressListAdapter;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.base.MyAppcomActivity;
import com.guaju.yueshangLife.bean.Goods;
import com.guaju.yueshangLife.bean.GoodsListTotal;
import com.guaju.yueshangLife.bean.MyAddressBean;
import com.guaju.yueshangLife.bean.SetDefaultBean;
import com.guaju.yueshangLife.databinding.ActivityMyAddressesBinding;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyAddressesActivity extends BaseActivity {

    private ActivityMyAddressesBinding binding;
    List<MyAddressBean.DataBean> addresses;
    private AddressListAdapter addressListAdapter;
    private LinearLayoutManager linearLayoutManager;
    private Intent intent;

    @Override
    protected void loadViewLayout() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_addresses);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void bindViews() {
        intent = getIntent();
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
    }

    @Override
    protected void onResume() {
        super.onResume();
        ApiEntity.meAddress();
    }

    private void loadAddress() {

    }

    @Override
    protected void setListener() {
        binding.topbar.getRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyAddressesActivity.this, AddAddressActivity.class));
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMyAddress(MyAddressBean myAddressBean) {
        if (myAddressBean == null) {
            showEmpty();
        } else {
            List<MyAddressBean.DataBean> data = myAddressBean.getData();
            if (data == null || data.isEmpty()) {
                showEmpty();
            } else {
                binding.emptyView.setVisibility(View.GONE);
                binding.recyclerview.setVisibility(View.VISIBLE);
                addresses = data;
                if (addressListAdapter==null){
                addressListAdapter = new AddressListAdapter(MyAddressesActivity.this, addresses);
                    addressListAdapter.setmOnItemClickListener(new AddressListAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(MyAddressBean.DataBean dataBean) {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable(CaculateActivity.SELECTED_ADDRESS,dataBean);
                            intent.putExtras(bundle);
                            setResult(CaculateActivity.RESAULT_ADDRESS,intent);
                            finish();
                        }

                        @Override
                        public void onItemLongClick(View v) {

                        }
                    });
                addressListAdapter.setOnSetDefaultClicked(new AddressListAdapter.OnSetDefaultClicked() {
                    @Override
                    public void onSetDefault(int id) {
                        ApiEntity.setDefaultAddress(id);
                    }
                });
                linearLayoutManager = new LinearLayoutManager(MyAddressesActivity.this);
                binding.recyclerview.setLayoutManager(linearLayoutManager);
                binding.recyclerview.setAdapter(addressListAdapter);
                }else{
                    addressListAdapter.notifyDataSetChanged();
                }
            }
        }

    }

    public void showEmpty() {
        binding.emptyView.setVisibility(View.VISIBLE);
        binding.recyclerview.setVisibility(View.GONE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSetDefault(SetDefaultBean bean) {
        if (bean==null){
            Toast.makeText(mContext, "设置默认地址失败", Toast.LENGTH_SHORT).show();
        }else{
        int code = bean.getCode();
        if (200==code){
            ToastUtil.show("设置默认地址成功");
            ApiEntity.meAddress();
        }
        }

    }



}
